<!--
 * @Author: daidai
 * @Date: 2022-02-28 16:29:08
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-09-27 15:05:45
 * @FilePath: \web-pc\src\pages\big-screen\components\echart\index.vue
-->
<template>
  <div :id="id" :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts';
import 'echarts-gl';

export default {
  name: 'echart',
  props: {
    className: {
      type: String,
      default: 'chart',
    },
    id: {
      type: String,
      default: 'chart',
    },
    width: {
      type: String,
      default: '100%',
    },
    height: {
      type: String,
      default: '100%',
    },
    options: {
      type: Object,
      default: () => ({}),
    },
    isClick: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    options: {
      handler(options) {
        // 设置true清空echart缓存
        this.chart.setOption(options, true);
      },
      deep: true,
    },
  },
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      // 初始化echart
      this.chart = echarts.init(this.$el);
      this.chart.setOption(this.options, true);
      if (this.isClick)
        // 绑定点击事件
        this.chart.on('click', (params) => {
          // console.log('点击的数据:', params);
          // // 可以获取到的数据信息
          // const {
          //   seriesName,  // 系列名称
          //   name,        // 类目名称
          //   value,       // 数值
          //   dataIndex    // 数据索引
          // } = params;

          // 这里处理你的点击逻辑
          this.$emit('chartClick', params);
        });
    },
  },
};
</script>

<style></style>
